#include "udp_io_loop.h"

namespace kratos {
namespace network {

UdpIoLoop::UdpIoLoop() {
}

UdpIoLoop::~UdpIoLoop() {
}

void UdpIoLoop::start(NetworkSelector * loop) {
    NetIoLoop::start(loop);
}

void UdpIoLoop::update() {
    loop_.update(
        [this](std::uint64_t id){
            onAccepted(id);
        },
        [this](std::uint64_t id, const char* data, std::size_t size) {
            onReceived(id, data, (std::uint32_t)size);
        }
    );
}

std::uint64_t UdpIoLoop::onNewAcceptor(const std::string & ip, const uint16_t port,
    std::size_t netBufSize) {
    return loop_.newAcceptor(ip, port, netBufSize);
}

std::uint64_t UdpIoLoop::onNewConnector(const std::string & ip, const uint16_t port,
    std::size_t netBufSize) {
    auto id = loop_.newConnector(ip, port, netBufSize);
    if (id) {
        onConnected(id);
    }
    return id;
}

void UdpIoLoop::onClose(std::uint64_t channelID) {
    loop_.close(channelID);
    onClosed(channelID);
}

void UdpIoLoop::onSend(std::uint64_t channelID, const char * data, std::uint32_t size) {
    loop_.send(channelID, data, size);
}

}
}

